package cn.fanyamin.dao.ibatis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.fanyamin.business.NoResultsFoundException;
import cn.fanyamin.dao.ItemDao;
import cn.fanyamin.domain.UserCategory;
import cn.fanyamin.domain.Item;



public class SqlMapItemDao extends CRUDDao<Integer,Item> implements ItemDao {
	protected String domainName = "Item";
	
	public SqlMapItemDao() {
		setSQLID(domainName);
	}

	public List<Item> findItems(Item item) {
		
		List<Item> list = (List<Item>) getSqlMapClientTemplate().queryForList(
				"getItemList", item);
		if (null == list) {
			throw new NoResultsFoundException("getItemList");
		}

		return list;
	}

	public UserCategory getCategory(int categoryID) {
		UserCategory ret = (UserCategory) getSqlMapClientTemplate().queryForObject(
				"retrieveCategory", categoryID);

		if (null == ret) {
			throw new NoResultsFoundException("retrieveCategory" + ", id=" + categoryID);
		}

		return ret;
	}

	public Map<Integer, UserCategory> getCategories(int categoryType,int userID) {

		Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("categoryType", categoryType);
        map.put("userID", userID);
		Map<Integer, UserCategory> ret = (Map<Integer, UserCategory>) getSqlMapClientTemplate().queryForMap("getCategories", map, "categoryID");

		if (null == ret) {
			throw new NoResultsFoundException("getCategories");
		}

		return ret;
	}

	public List<UserCategory> getCategories(int userID) {
		List<UserCategory> ret = (List<UserCategory>) getSqlMapClientTemplate().queryForList("getUserCategories", userID);

		if (null == ret) {
			throw new NoResultsFoundException("getUserCategories");
		}

		return ret;
	}
	
	public List<Item> getCheckList(int userID) {
       
		List<Item> list = (List<Item>) getSqlMapClientTemplate().queryForList(
				"getCheckList", userID);
		if (null == list) {
			throw new NoResultsFoundException("getCheckList");
		}

		return list;
	}

	public int createCategory(UserCategory category) {
		int ret = (Integer)getSqlMapClientTemplate().insert("createCategory", category);
		return ret;
	}

	public int deleteCategory(int categoryID) {
		return getSqlMapClientTemplate().delete("deleteCategory", categoryID);
	}

	public int updateCategory(UserCategory category) {
		return getSqlMapClientTemplate().delete("updateCategory", category);
	}

	public List<UserCategory> getCategoryList(int categoryType, int userID) {
		Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("categoryType", categoryType);
        map.put("userID", userID);
        
		List<UserCategory> ret = (List<UserCategory>) getSqlMapClientTemplate().queryForList("getUserCategoriesByType", map);

		if (null == ret) {
			throw new NoResultsFoundException("getUserCategoriesByType");
		}

		return ret;
	}
	
}
